{{define "head"}}
<style>
    .form-input:focus {
        box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
    }
    .loading {
        display: none;
    }
    .loading.active {
        display: block;
    }
</style>
{{end}}

{{define "content"}}
<div class="max-w-7xl mx-auto py-6 sm:px-6 lg:px-8">
    <div class="bg-white rounded-lg shadow-sm p-6">
        <h1 class="text-2xl font-bold text-gray-900 mb-6">生成文章</h1>
        
        <form id="generateForm" class="space-y-6">
            <div>
                <label for="keywords" class="block text-sm font-medium text-gray-700">关键词（每行一个）</label>
                <div class="mt-1">
                    <textarea name="keywords" id="keywords" required rows="5"
                        class="form-input block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 sm:text-sm"
                        placeholder="请输入关键词，每行一个"></textarea>
                    <p class="mt-1 text-sm text-gray-500">每个关键词将生成独立的文章，失败的关键词不会影响其他关键词的生成。</p>
                </div>
            </div>
            <div>
                <label for="keywords" class="block text-sm font-medium text-gray-700">标签id</label>
                <div class="mt-1">
                    <input type="text" name="tag_ids" id="tag_ids"
                        class="form-input block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 sm:text-sm"
                        placeholder="请输入标签id,多个标签id用逗号分隔">
                </div>
            </div>

            <div>
                <label for="category" class="block text-sm font-medium text-gray-700">文章栏目</label>
                <div class="mt-1">
                    <select name="category" id="category"
                        class="form-select block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 sm:text-sm">
                        <option value="">使用默认栏目</option>
                        {{range .categories}}
                        <option value="{{.ID}}">{{.Name}}</option>
                        {{end}}
                    </select>
                </div>
            </div>

            <div>
                <label for="count" class="block text-sm font-medium text-gray-700">每个关键词生成数量</label>
                <div class="mt-1">
                    <select name="count" id="count" required
                        class="form-select block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 sm:text-sm">
                        <option value="1">1篇</option>
                    </select>
                </div>
            </div>

            <div>
                <label for="publish" class="block text-sm font-medium text-gray-700">是否发布</label>
                <div class="mt-1">
                    <input type="checkbox" name="publish" value="true">
                </div>
            </div>
            <div class="flex justify-end">
                <button type="submit"
                    class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
                    <i class="fas fa-magic mr-2"></i>开始生成
                </button>
            </div>
        </form>

        <!-- 加载动画和进度显示 -->
        <div class="loading mt-6">
            <div class="flex items-center justify-center mb-4">
                <div class="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600"></div>
                <span class="ml-2 text-gray-600">正在生成文章，请稍候...</span>
            </div>
            <div id="progressInfo" class="text-sm text-gray-600 text-center"></div>
        </div>
    </div>
</div>
{{end}}

{{define "scripts"}}
<script>
document.getElementById('generateForm').addEventListener('submit', async function(e) {
    e.preventDefault();
    
    const form = e.target;
    const loading = document.querySelector('.loading');
    const progressInfo = document.getElementById('progressInfo');
    const submitButton = form.querySelector('button[type="submit"]');
    
    // 显示加载动画
    loading.classList.add('active');
    submitButton.disabled = true;
    
    try {
        const formData = new FormData(form);
        const response = await fetch('/generate', {
            method: 'POST',
            body: formData
        });
        
        if (response.ok) {
            const result = await response.json();
            if (result.success) {
                alert(`生成完成！\n成功：${result.success_count} 个\n失败：${result.failed_count} 个`);
                if (result.failed_keywords && result.failed_keywords.length > 0) {
                    const failedKeywords = result.failed_keywords.join('\n');
                    alert(`以下关键词生成失败，请重试：\n${failedKeywords}`);
                }
                // 生成成功后跳转到历史记录页面
                window.location.href = '/history';
            } else {
                alert('生成失败，请重试');
            }
        } else {
            alert('生成失败，请重试');
        }
    } catch (error) {
        console.error('Error:', error);
        alert('生成失败，请重试');
    } finally {
        // 隐藏加载动画
        loading.classList.remove('active');
        submitButton.disabled = false;
        progressInfo.textContent = '';
    }
});
</script>
{{end}}